package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.RedBag;
import com.bootdo.system.domain.RoleDO;
import com.bootdo.system.service.RoleService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RequestMapping("/sys/role")
@Controller
@Api(value = "/sys/role", description = "角色操作接口")
public class RoleController extends BaseController {
	String prefix = "sys/role";
	@Autowired
	RoleService roleService;


	@ApiOperation(value="查询", notes="根据条件查询", response=String.class)
	@GetMapping("/list")
	@ResponseBody
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "角色id", required = false, paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "pageNum", value = "当前第几页", required = true,paramType="query", dataType = "int"),
        @ApiImplicitParam(name = "pageSize", value = "每页显示多少条", required = true,paramType="query", dataType = "int")
	})
	R list(@RequestParam Map<String,Object> param)  throws Exception{
		Query query = new Query(param);
		List<RoleDO> babys = roleService.list(query);
		for(RoleDO role:babys) {
			role.setMenuIds(roleService.getMenuList(role.getRoleId()));
		}
		PageInfo<RoleDO> pageInfo = new PageInfo<RoleDO>(babys);
		int total = (int)pageInfo.getTotal();
		PageUtils pageUtil = new PageUtils(babys, total);
		return new R("list",pageUtil);
	}


	@ApiOperation(value="角色详情", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "角色id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/edit")
	@ResponseBody
	R edit(Long id)  throws Exception{
		RoleDO roleDO = roleService.get(id);
		return new R("obj",roleDO);
	}

	@ApiOperation(value="角色保存", notes="", response=String.class)
	@PostMapping("/save")
	@ResponseBody()
	R save(@RequestBody RoleDO role)  throws Exception{
		if (roleService.save(role) > 0) {
			return R.saveOk(role.getRoleId());
		} else {
			return R.error(1, "保存失败");
		}
	}

	@ApiOperation(value="角色更新", notes="", response=String.class)
	@PostMapping("/update")
	@ResponseBody
	R update(@RequestBody RoleDO role)  throws Exception{
		if (roleService.update(role) > 0) {
			return R.ok();
		} else {
			return R.error(1, "保存失败");
		}
	}

	@ApiOperation(value="删除", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "角色id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/remove")
	@ResponseBody
	R remove(Long id)  throws Exception{
		if (roleService.remove(id) > 0) {
			return R.ok();
		} else {
			return R.error(1, "删除失败");
		}
	}
}
